<template>
  <div class="box">
    <div class="searcharea">
      <!-- 搜索 -->
      <el-row>
        <el-col :span="24">
          <div class="query">
            <el-form :inline="true" :model="queryWorkStationParam" class="demo-form-inline">
              <el-form-item label="工作站名称">
                <el-input
                  v-model="queryWorkStationParam.WorkStationName"
                  placeholder="请输入"
                  clearable
                  style="width: 200px"
                />
              </el-form-item>

              <el-form-item>
                <el-button type="primary" :icon="Search" @click="SearchWorkStation()">搜索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="contentarea">
      <!-- 功能按钮 -->
      <el-row>
        <el-col :span="24">
          <div class="functionbutton">
            <el-button type="primary" plain @click="addWorkStation()">添加</el-button>
            <el-button type="danger" plain @click="batchDeletionWorkStation()">批量删除</el-button>
          </div>
        </el-col>
        <el-dialog v-model="centerDialogVisible" title="添加工作站" width="30%" align-center>
          <el-form
            ref="CreateWorkStationForm"
            :model="CreateWorkStationForm"
            :rules="CreateWorkStationRules"
            label-width="auto"
            style="padding: 20px"
          >
            <el-form-item label="工作站编号">
              <el-input v-model="CreateWorkStationForm.WorkStationNo" placeholder="不输入则自动生成"> </el-input>
            </el-form-item>
            <el-form-item prop="WorkStationName" label="工作站名称">
              <el-input v-model="CreateWorkStationForm.WorkStationName" placeholder="请输入工作站名称"> </el-input>
            </el-form-item>
            <el-form-item label="所在产线" prop="ProductionLineId">
              <el-select v-model="CreateWorkStationForm.ProductionLineId" filterable placeholder="请选择" clearable>
                <el-option
                  v-for="option in ProductionOptions"
                  :key="option.key"
                  :label="option.productionLineName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="所属工序" prop="ProcessId">
              <el-select v-model="CreateWorkStationForm.ProcessId" filterable placeholder="请选择" clearable>
                <el-option
                  v-for="option in ProcessOptions"
                  :key="option.key"
                  :label="option.processName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="设备" prop="EquipmentManagementId">
              <el-select
                v-model="CreateWorkStationForm.EquipmentManagementId"
                filterable
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="option in EquipmentManagementOptions"
                  :key="option.key"
                  :label="option.equipmentName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
          </el-form>

          <template #footer>
            <div class="dialog-footer">
              <el-button @click="centerDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="CreateWorkStationDialog()">确认</el-button>
            </div>
          </template>
        </el-dialog>
      </el-row>

      <!-- 数据表格 -->
      <el-row>
        <el-col :span="24">
          <div>
            <el-table
              v-loading="tableloading"
              :data="tableData"
              border
              style="width: 100%"
              max-height="400px"
              :selection="selectedRows"
              @selection-change="handleSelectionChange"
              @row-click="getRowkey"
              show-overflow-tooltip
              empty-text="暂无数据"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column type="index" label="序号" />
              <el-table-column prop="workStationNo" label="工作站编码" width="180" />
              <el-table-column prop="workStationName" label="工作站名称" width="150" />
              <el-table-column prop="equipmentManagement.equipmentName" label="设备名称" width="150" />
              <el-table-column prop="productionLine.productionLineNo" label="产线编号" width="180" />
              <el-table-column prop="productionLine.productionLineName" label="产线名称" width="160" />
              <el-table-column prop="process.processName" label="工序名称" width="160" />
              <el-table-column prop="creator" label="创建人" />
              <el-table-column prop="updator" label="修改人" />
              <el-table-column prop="createtime" label="创建时间" width="180" :formatter="formatColumnTime" />
              <el-table-column prop="updateDate" label="更新时间" width="180" :formatter="formatColumnTime" />
              <el-table-column fixed="right" label="操作" width="140">
                <template #default="scope">
                  <el-button type="danger" plain size="small" @click="DeleteWorkStationSingle(scope.row.id)">
                    删除
                  </el-button>

                  <el-button type="primary" plain size="small" @click="UpdateWorkStation(scope.row)">更新</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>

        <el-dialog v-model="UpdateDialogVisible" title="修改工作站" width="30%" align-center>
          <el-form
            ref="UpdateWorkStationForm"
            :model="UpdateWorkStationForm"
            :rules="CreateWorkStationRules"
            label-width="auto"
            style="padding: 20px"
          >
            <el-form-item label="工作站编号">
              <el-input v-model="UpdateWorkStationForm.WorkStationNo" disabled placeholder="不输入则自动生成">
              </el-input>
            </el-form-item>
            <el-form-item prop="WorkStationName" label="工作站名称">
              <el-input v-model="UpdateWorkStationForm.WorkStationName" placeholder="请输入工作站名称"> </el-input>
            </el-form-item>

            <el-form-item label="所在产线" prop="ProductionLineId">
              <el-select v-model="UpdateWorkStationForm.ProductionLineId" filterable placeholder="请选择" clearable>
                <el-option
                  v-for="option in ProductionOptions"
                  :key="option.key"
                  :label="option.productionLineName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="所属工序" prop="ProcessId">
              <el-select v-model="UpdateWorkStationForm.ProcessId" filterable placeholder="请选择" clearable>
                <el-option
                  v-for="option in ProcessOptions"
                  :key="option.key"
                  :label="option.processName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="设备" prop="EquipmentManagementId">
              <el-select
                v-model="UpdateWorkStationForm.EquipmentManagementId"
                filterable
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="option in EquipmentManagementOptions"
                  :key="option.key"
                  :label="option.equipmentName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
          </el-form>

          <template #footer>
            <div class="dialog-footer">
              <el-button @click="UpdateDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="UpdateWorkStationDialog()">确认</el-button>
            </div>
          </template>
        </el-dialog>
      </el-row>

      <!-- 分页 -->
      <el-row>
        <el-col :span="24">
          <div class="demo-pagination-block" style="display: flex; justify-content: flex-end">
            <el-config-provider :locale="zhCn">
              <el-pagination
                v-model:current-page="queryWorkStationParam.PageIndex"
                v-model:page-size="queryWorkStationParam.PageSize"
                :page-sizes="[100, 200, 300, 400]"
                :small="false"
                :disabled="false"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="queryWorkStationParam.totalCount"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              >
              </el-pagination>
            </el-config-provider>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script >
import { Search } from '@element-plus/icons-vue'

import { formatTime, capitalizeFirstLetter } from '../../utils/index'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { ElMessageBox, ElNotification } from 'element-plus'
import {
  ApiGetShopfloor_WorkStationPageListAsync,
  ApiDeletionShopfloor_WorkStationAsync,
  ApiCreateShopfloor_WorkStationAsync,
  ApiUpdateShopfloor_WorkStationAsync
} from '../../api/shopfloor/ShopfloorWorkStationApi'
import { ApiGetProcessRoute_ProcessPageListAsync } from '../../api/processRoute/ProcessRouteProcessApi'
import { ApiGetShopfloor_ProductionLinePageListAsync } from '../../api/shopfloor/ShopfloorProductionLineApi'
import { ApiGetEquipment_EquipmentManagementPageListAsync } from '../../api/equipment/EquipmentManagementApi'

export default {
  name: 'WorkStationView',
  data() {
    return {
      ProductionOptions: [],
      ProcessOptions: [],
      tableloading: false,
      UpdateDialogVisible: false,
      UpdateWorkStationForm: {},
      EquipmentManagementOptions: [],
      CreateWorkStationForm: {},
      CreateWorkStationRules: {
        WorkStationName: [{ required: true, trigger: 'blur', message: '工作站名称不能为空' }]
      },
      ruleForm: {
        name: ''
      },
      centerDialogVisible: false,
      selectedRows: [], // 存储选中的行数据
      queryWorkStationParam: {
        WorkStationName: null,
        PageIndex: 1,
        PageSize: 100,
        totalCount: 0
      },
      queryProductionLineParam: {
        PageIndex: 1,
        PageSize: 100,
        totalCount: 0
      },
      queryEquipmentManagementParam: {
        PageIndex: 1,
        PageSize: 100,
        totalCount: 0
      },
      queryProcessParam: {
        PageIndex: 1,
        PageSize: 100,
        totalCount: 0
      },
      tableData: [] // 表格数据
    }
  },
  setup() {
    return {
      Search,
      zhCn
    }
  },
  created() {
    this.SearchWorkStation()
  },
  methods: {
    //确认更新
    UpdateWorkStationDialog() {
      this.$refs.UpdateWorkStationForm.validate((valid) => {
        if (valid) {
          ApiUpdateShopfloor_WorkStationAsync(this.UpdateWorkStationForm).then((res) => {
            if (res.data.data) {
              ElNotification({
                message: '更新成功',
                offset: 100,
                type: 'success',
                duration: 2000
              })
              this.UpdateDialogVisible = false
              this.SearchWorkStation()
            }
          })
        }
      })
    },
    //更新工作站
    UpdateWorkStation(res) {
      this.UpdateDialogVisible = true
      this.UpdateWorkStationForm = capitalizeFirstLetter(res)

      //工序
      ApiGetProcessRoute_ProcessPageListAsync(this.queryProcessParam).then((res) => {
        this.ProcessOptions = res.data.data
        this.queryProcessParam.PageIndex = res.data.pageIndex
        this.queryProcessParam.PageSize = res.data.pageSize
        this.queryProcessParam.totalCount = res.data.totalCount
      })
      // 获取产线列表
      ApiGetShopfloor_ProductionLinePageListAsync(this.queryProductionLineParam).then((res) => {
        this.ProductionOptions = res.data.data
        this.queryProductionLineParam.PageIndex = res.data.pageIndex
        this.queryProductionLineParam.PageSize = res.data.pageSize
        this.queryProductionLineParam.totalCount = res.data.totalCount
      })
      //设备
      ApiGetEquipment_EquipmentManagementPageListAsync(this.queryEquipmentManagementParam).then((res) => {
        this.EquipmentManagementOptions = res.data.data
        this.queryEquipmentManagementParam.PageIndex = res.data.pageIndex
        this.queryEquipmentManagementParam.PageSize = res.data.pageSize
        this.queryEquipmentManagementParam.totalCount = res.data.totalCount
      })
    },
    //删除工作站
    DeleteWorkStationSingle(res) {
      this.selectedRows = []
      this.selectedRows.push(res)
      this.batchDeletionWorkStation()
    },
    //添加工作站
    CreateWorkStationDialog() {
      this.$refs.CreateWorkStationForm.validate((valid) => {
        if (valid) {
          ApiCreateShopfloor_WorkStationAsync(this.CreateWorkStationForm).then((res) => {
            if (res.data.data) {
              ElNotification({
                message: '添加工作站成功',
                offset: 100,
                type: 'success',
                duration: 2000
              })
              this.centerDialogVisible = false
              this.SearchWorkStation()
            }
          })
        }
      })
    },

    //获取用户选择项
    handleSelectionChange(select) {
      // 提取传递过来的对象中的"id"字段，并存储到this.selectedRows中
      this.selectedRows = select.map((item) => item.id)
    },
    //添加工作站
    addWorkStation() {
      this.CreateWorkStationForm = {}
      this.centerDialogVisible = true

      //工序
      ApiGetProcessRoute_ProcessPageListAsync(this.queryProcessParam).then((res) => {
        this.ProcessOptions = res.data.data
        this.queryProcessParam.PageIndex = res.data.pageIndex
        this.queryProcessParam.PageSize = res.data.pageSize
        this.queryProcessParam.totalCount = res.data.totalCount
      })
      //产线
      ApiGetShopfloor_ProductionLinePageListAsync(this.queryProductionLineParam).then((res) => {
        this.ProductionOptions = res.data.data
        this.queryProductionLineParam.PageIndex = res.data.pageIndex
        this.queryProductionLineParam.PageSize = res.data.pageSize
        this.queryProductionLineParam.totalCount = res.data.totalCount
      })
      //设备
      ApiGetEquipment_EquipmentManagementPageListAsync(this.queryEquipmentManagementParam).then((res) => {
        this.EquipmentManagementOptions = res.data.data
        this.queryEquipmentManagementParam.PageIndex = res.data.pageIndex
        this.queryEquipmentManagementParam.PageSize = res.data.pageSize
        this.queryEquipmentManagementParam.totalCount = res.data.totalCount
      })
    },
    //指删除工作站
    batchDeletionWorkStation() {
      if (this.selectedRows.length < 1) {
        ElNotification({
          message: '请先选择一条或更多信息',
          type: 'warning',
          offset: 100,
          duration: 2000
        })
        return
      }
      ElMessageBox.confirm('确实要删除这些信息吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          ApiDeletionShopfloor_WorkStationAsync(this.selectedRows)
            .then((res) => {
              if (res.data.data) {
                ElNotification({
                  message: '删除成功',
                  offset: 100,
                  type: 'success',
                  duration: 2000
                })
                this.selectedRows = []
                this.SearchWorkStation()
              }
            })
            .catch(() => {
              this.selectedRows = []
            })
        })
        .catch(() => {
          this.selectedRows = []
        })
    },
    //时间转换
    formatColumnTime(row, column, time) {
      return formatTime(time)
    },

    //每页条数
    handleSizeChange(e) {
      this.queryWorkStationParam.PageSize = e
      this.SearchWorkStation()
    },

    // 搜索
    SearchWorkStation() {
      this.tableloading = true
      ApiGetShopfloor_WorkStationPageListAsync(this.queryWorkStationParam)
        .then((res) => {
          this.tableData = res.data.data
          this.queryWorkStationParam.PageIndex = res.data.pageIndex
          this.queryWorkStationParam.PageSize = res.data.pageSize
          this.queryWorkStationParam.totalCount = res.data.totalCount
        })
        .finally(() => {
          this.tableloading = false
        })
    }
  }
}
</script>
<style lang="less"  scoped>
@import '../../assets/globle/index.css';
</style>